{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "from common import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "RESULT_JSON = \"/Users/law/repos/viper/results/kv_size/all_kv_size_revision.json\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "FIXTURES = [VIPER, DASH, HYBRID_FASTER, PMEMKV, CRL_STORE]\n",
    "BM_TYPES = ['insert', 'get']\n",
    "\n",
    "BMS = get_all_runs(RESULT_JSON)\n",
    "# pprint(BMS) \n",
    "\n",
    "KV_SIZE_RE = re.compile(r\".*KeyType(\\d+),ValueType(\\d+).*\")\n",
    "\n",
    "def get_kv_size(bm_name):\n",
    "    match = KV_SIZE_RE.match(bm_name)\n",
    "    if match is None:\n",
    "        raise f\"unknown kv size: {bm_name}\"\n",
    "    return (int(match.group(1)), int(match.group(2)))\n",
    "\n",
    "RUNS = defaultdict(list)\n",
    "\n",
    "for bm in BMS:\n",
    "    for f, _ in FIXTURES:\n",
    "        if f in bm['name']:            \n",
    "            bm_type = get_bm_type(bm['name'])\n",
    "            kv_size = get_kv_size(bm['name'])\n",
    "            if kv_size == (16, 100): continue\n",
    "            RUNS[(f, bm_type)].append((kv_size, bm))\n",
    "        \n",
    "    for _, run in RUNS.items():\n",
    "        run.sort(key=lambda x: x[0]) \n",
    "        \n",
    "# pprint(RUNS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, (insert_ax, get_ax) = plt.subplots(1, 2, figsize=(10, 3))\n",
    "\n",
    "\n",
    "def get_bm_ax(bm_type):\n",
    "    if bm_type == 'insert': return insert_ax\n",
    "    if bm_type == 'get': return get_ax\n",
    "    raise RuntimeError(f\"Unknown bm_type {bm_type}\")\n",
    "\n",
    "sizes = [16, 116, 216, 1000]\n",
    "sizes = [16, 216, 532, 1000]\n",
    "# sizes = [1, 2, 3, 4]\n",
    "\n",
    "for bm_type, letter in zip(BM_TYPES, ['a', 'b']):\n",
    "    ax = get_bm_ax(bm_type)\n",
    "    ax.set_title(f\"({letter}) {bm_type.upper()}\", fontsize=20)\n",
    "    for (fixture, _) in FIXTURES:\n",
    "        r = [run[1]['items_per_second'] / MILLION for run in RUNS[(fixture, bm_type)]]\n",
    "        style = STYLES[fixture]\n",
    "        x_vals = sizes[1:] if fixture == PMEMKV[0] or fixture == CRL_STORE[0] else sizes\n",
    "        ax.plot(x_vals, r, marker=style.marker, ms=8, #ms=style.marker_size, \n",
    "                color=style.color, markeredgewidth=0.5, lw=2)\n",
    "    ax.set_xticks(sizes)\n",
    "    ax.set_xticklabels([str(x) for x in sizes])\n",
    "    ax.set_xticklabels(['8/8', '16/200', '32/500', '100/900'])\n",
    "    ax.grid(axis='y', which='major')\n",
    "    for tick in ax.xaxis.get_major_ticks():\n",
    "        tick.label.set_fontsize(19)\n",
    "    for tick in ax.yaxis.get_major_ticks():\n",
    "        tick.label.set_fontsize(19)\n",
    "\n",
    "\n",
    "insert_ax.set_ylabel(\"Throughput (Mops/s)\", fontsize=18)\n",
    "insert_ax.set_title(f\"(a) PUT\", fontsize=20)\n",
    "insert_ax.set_ylim(0, 22)\n",
    "get_ax.set_ylim(0, 45)\n",
    "\n",
    "fig.text(0.542, -0.05, \"Key/Value Record Size in Byte\", ha='center', fontsize=20)\n",
    "\n",
    "fig.legend(loc='upper center', labels=[f[1] for f in FIXTURES], \n",
    "           bbox_to_anchor=(0.5, 1.13), ncol=6, frameon=False, fontsize=18,\n",
    "           columnspacing=1, handletextpad=0.3)\n",
    "\n",
    "plt.tight_layout()\n",
    "for ax in (insert_ax, get_ax):\n",
    "    hide_border(ax, True)\n",
    "    \n",
    "fig.savefig('charts/kv_size.pdf', bbox_inches='tight')\n",
    "fig.savefig('charts/kv_size.svg', bbox_inches='tight')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
